﻿Imports Soldata.Azox.Models

Namespace Snippets

    <Authorize>
    Public Class AdminController
        Inherits AzoxController

        '
        ' GET: /snippets/admin

        Function Index(Optional pageIndex As Integer = 0) As ActionResult
            Dim pageSize As Double = My.Settings.PageSize
            Dim totalRecords As Double = Db.Snippets.Count
            ViewBag.PageIndex = pageIndex
            ViewBag.PageCount = CInt(Math.Ceiling(totalRecords / pageSize))

            ViewBag.Title = "Список сниппетов"

            Return View("admin", Db.Snippets.OrderBy(Function(m) m.Title).Skip(pageIndex * CInt(pageSize)).Take(CInt(pageSize)).ToList())
        End Function

        Function Edit(id As Guid) As ActionResult
            ViewBag.Title = "Редактирование сниппета"

            Return View(Db.Snippets.Find(id))
        End Function

        <HttpPost()>
        <ValidateAntiForgeryToken()>
        Function Edit(model As Snippet) As ActionResult
            If ModelState.IsValid Then
                Db.Entry(model).State = EntityState.Modified
                Db.SaveChanges()

                Return RedirectToAction("index")
            End If

            ViewBag.Title = "Редактирование сниппета"

            Return View(model)
        End Function

    End Class

End Namespace
